package com.bookmanagement.utils;

import com.bookmanagement.constant.BcryptConstant;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

public class BcryptUtil {
    // 线程安全，可复用
    private static final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(BcryptConstant.DIGEST_STRENGTH);

    /**
     * 使用 BCrypt 加密明文密码
     *
     * @param rawPassword 明文密码
     * @return 加密后的哈希字符串（包含随机盐）
     * @author liuke
     */
    public static String encrypt(String rawPassword) {
        if (rawPassword == null || rawPassword.isEmpty()) {
            throw new IllegalArgumentException("密码不能为空");
        }
        return encoder.encode(rawPassword);
    }


    public static boolean checkPassword(String plainPassword, String hashedPassword) {
        return encoder.matches(plainPassword, hashedPassword);
    }
}
